//
//  XS_KCXianxiaVC.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/31.
//

import UIKit

class XS_KCXianxiaVC: XS_KCBaseVC {
    override func onAction(_ type: XS_KCBaseVC.ActionType) {
        guard let model = model else { return }
        switch type {
        case .分享:
            guard let url = URL(string: "/course?id=\(model.curriculum.id)".spliceH5Api()) else { return }
            let activityVc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
            XS_NVC.shared.present(activityVc, animated: true)
        case .按钮:
            guard XS_Singleton.shared.infoModel != nil else {
                XS_NVC.shared.toLogin(false)
                return
            }
            guard model.curriculum.isBuy != "yes" else {
                view.xsShowMsg("您已报名请等待")
                return
            }
            let alert = XS_InfoBottomAlertVC()
            alert.actionSubject
                .subscribe(onNext: { [weak self, weak alert] type, completion in
                    completion?(nil)
                    guard let `self` = self, let alert = alert, type == .sure else {
                        return
                    }
                    
                    let buyVC = XS_BuyVC()
                    buyVC.courseId = "\(model.curriculum.id)"
                    buyVC.courseClassification = "offline"
                    buyVC.name = alert.name.text
                    buyVC.remark = alert.beizhu.text
                    XS_NVC.shared.pushViewController(buyVC, animated: true)
                    
//                    XS_NetWork.requestJson("/order/payMoneyByOrder", parameters: [
//                        "courseClassification" : "offline",
//                        "courseId":self.id,
//                        "name":alert.name.text ?? "",
//                        "remark":alert.beizhu.text ?? ""
//                    ]).subscribe(onNext: { [weak self] resp in
//                        self?.view.xsShowMsg(resp.msg)
//                        if resp.success {
//                            completion?(nil)
//                        }
//                    }).disposed(by: self.disposeBag)
                }).disposed(by: self.disposeBag)
            present(alert, animated: false)
        default: super.onAction(type)
        }
    }
    override func upScroll(_ top: UIView) {
        guard let model = model else { return }
        var last = top
        
        do {
            let view = UIView()
            view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
            ss.addSubview(view)
            view.snp.makeConstraints({
                $0.left.width.equalToSuperview()
                $0.top.equalTo(last.snp.bottom)
                $0.bottom.lessThanOrEqualToSuperview()
            })
            
            let line = UIView()
            line.backgroundColor = #colorLiteral(red: 0.9019607843, green: 0.9137254902, blue: 0.9450980392, alpha: 1)
            view.addSubview(line)
            line.snp.makeConstraints({
                $0.top.equalToSuperview().offset(.scale*12)
                $0.height.equalTo(1)
                $0.left.equalTo(.scale*16)
                $0.right.equalTo(-.scale*16)
            })
            
            let imgV1 = UIImageView(image: UIImage(named: "icon_24_地点备份"))
            view.addSubview(imgV1)
            imgV1.snp.makeConstraints({
                $0.width.height.equalTo(.scale*12)
                $0.left.equalTo(.scale*16)
                $0.top.equalTo(line.snp.bottom).offset(.scale*14)
            })
            let label1 = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1))
            view.addSubview(label1)
            label1.snp.makeConstraints({
                $0.left.equalTo(imgV1.snp.right).offset(.scale*4)
                $0.centerY.equalTo(imgV1.snp.centerY)
            })
            
            let imgV2 = UIImageView(image: UIImage(named: "icon_12px_定位"))
            view.addSubview(imgV2)
            imgV2.snp.makeConstraints({
                $0.width.height.equalTo(.scale*12)
                $0.left.equalTo(.scale*16)
                $0.top.equalTo(imgV1.snp.bottom).offset(.scale*10)
                $0.bottom.lessThanOrEqualTo(-.scale*14)
            })
            let label2 = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1), text: model.curriculum.offlineDetailedAddress)
            view.addSubview(label2)
            label2.snp.makeConstraints({
                $0.left.equalTo(imgV2.snp.right).offset(.scale*4)
                $0.centerY.equalTo(imgV2.snp.centerY)
            })
            
            let fm = DateFormatter()
            fm.dateFormat = "yyyy-MM-dd HH:mm:ss"
            if let startDate = fm.date(from: model.curriculum.startTime), let endDate = fm.date(from: model.curriculum.endTime) {
                let fm2 = DateFormatter()
                fm2.dateFormat = "MM月dd日 HH:mm"
                label1.text = fm2.string(from: startDate) + " - " + fm2.string(from: endDate)
            }
            
            last = view
        }
        
        if model.curriculum.teacherBOList.count > 0 {
            let teacher = XS_KCTeachers()
            ss.addSubview(teacher)
            teacher.snp.makeConstraints({
                $0.left.width.equalToSuperview()
                $0.top.equalTo(last.snp.bottom)
                $0.bottom.lessThanOrEqualToSuperview()
            })
            teacher.modelPS.onNext(model.curriculum.teacherBOList)
            last = teacher
        }
        
        if let item = model.curriculum.curriculumCertificateDto, item.id > 0 {
            let zhengshu = XS_KCZhengshu()
            ss.addSubview(zhengshu)
            zhengshu.snp.makeConstraints({
                $0.left.width.equalToSuperview()
                $0.top.equalTo(last.snp.bottom)
                $0.bottom.lessThanOrEqualToSuperview()
            })
//            zhengshu.modelPS.onNext(("可获证书", item.imgUrl))
            zhengshu.zhengshuNameModelPS.onNext(("可获证书", item.name))
            let tap = UITapGestureRecognizer()
            zhengshu.addGestureRecognizer(tap)
            tap.rx.event.asDriver()
                .drive(onNext: { _ in
                    let vc = XS_ZhengshuDetailVC()
                    vc.id = item.id
                    XS_NVC.shared.pushViewController(vc, animated: true)
                }).disposed(by: disposeBag)
            last = zhengshu
        }
        
        if model.curriculum.descriptionImgUrl.count > 0 {
            let zhengshu = XS_KCZhengshu()
            ss.addSubview(zhengshu)
            zhengshu.snp.makeConstraints({
                $0.left.width.equalToSuperview()
                $0.top.equalTo(last.snp.bottom).offset(.scale*8)
                $0.bottom.lessThanOrEqualToSuperview().offset(-.scale*8)
            })
            zhengshu.modelPS.onNext(("活动介绍", model.curriculum.descriptionImgUrl))
            last = zhengshu
        }
        
        if model.curriculum.isBuy == "yes" {
            self.btnTypePS.onNext(("已报名", #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
        } else {
            self.btnTypePS.onNext(("立即报名", #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)))
        }
        dataPS.subscribe(onNext: { [weak self] model in
            guard let `self` = self else { return }
            if model.curriculum.isBuy == "yes" {
                self.btnTypePS.onNext(("已报名", #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)))
            } else {
                self.btnTypePS.onNext(("立即报名", #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)))
            }
        }).disposed(by: disposeBag)
    }
}
